<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Generate random key for sodium_crypto_secretbox</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.sodium-crypto-scalarmult.html">« sodium_crypto_scalarmult</a></li>
      <li style="float: right;"><a href="function.sodium-crypto-secretbox-open.html">sodium_crypto_secretbox_open »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.sodium.html">Sodium 函数</a></li>
    <li>Generate random key for sodium_crypto_secretbox</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.sodium-crypto-secretbox-keygen" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">sodium_crypto_secretbox_keygen</h1>
  <p class="verinfo">(PHP 7 &gt;= 7.2.0, PHP 8)</p><p class="refpurpose"><span class="refname">sodium_crypto_secretbox_keygen</span> &mdash; <span class="dc-title">Generate random key for sodium_crypto_secretbox</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.sodium-crypto-secretbox-keygen-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>sodium_crypto_secretbox_keygen</strong></span>(): <span class="type">string</span></div>

  <p class="para rdfs-comment">
   Generate a key for use with <span class="function"><a href="function.sodium-crypto-secretbox.html" class="function">sodium_crypto_secretbox()</a></span> and <span class="function"><a href="function.sodium-crypto-secretbox-open.html" class="function">sodium_crypto_secretbox_open()</a></span>.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.sodium-crypto-secretbox-keygen-parameters">
  <h3 class="title">参数</h3>
  <p class="para">此函数没有参数。</p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.sodium-crypto-secretbox-keygen-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns the generated string of cryptographically secure random bytes.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.sodium-crypto-secretbox-keygen-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-992">
    <p><strong>示例 #1 <span class="function"><strong>sodium_crypto_secretbox_keygen()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$key&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">sodium_crypto_secretbox_keygen</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">sodium_bin2hex</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$key&nbsp;</span><span style="color: #007700">)&nbsp;);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
string(64) &quot;88bd1dc51ec81984f3ddc5a8f59a3d95b647e2da3e879f1b9ceb0abd89e7286c&quot;
</pre></div>
    </div>
   </div>

   <div class="example" id="example-993">
    <p><strong>示例 #2 
     Comparing <span class="function"><strong>sodium_crypto_secretbox_keygen()</strong></span>
     with <span class="function"><a href="function.random-bytes.html" class="function">random_bytes()</a></span>
    </strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$key&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">sodium_crypto_secretbox_keygen</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$bytes&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">random_bytes</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">SODIUM_CRYPTO_SECRETBOX_KEYBYTES&nbsp;</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">mb_strlen</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$key</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'8bit'&nbsp;</span><span style="color: #007700">)&nbsp;===&nbsp;</span><span style="color: #0000BB">mb_strlen</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$bytes</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'8bit'&nbsp;</span><span style="color: #007700">)&nbsp;);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
bool(true)
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.sodium-crypto-secretbox-keygen-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.sodium-bin2hex.html" class="function" rel="rdfs-seeAlso">sodium_bin2hex()</a> - Encode to hexadecimal</span></li>
    <li class="member"><span class="function"><a href="function.random-bytes.html" class="function" rel="rdfs-seeAlso">random_bytes()</a> - Get cryptographically secure random bytes</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>